<?php
/**
* CORE PACKAGE
* This is where the main every day functions and objects for FW are determined.
* This object was originally named dm in honour of my comapny Doutromundo.
*
* @link http://framework.doutromundo.com/
* @copyright Copyright: 2003-2004 Doutromundo
* @author Gregory Brown <bugzbrown@gmail.com>
* @access public
* @package core
*/
/**
*	DM CLASS
*
*	Used to control the load and error
*	They used to be two separate classes which have been merged into one
*	to make it easier for developers to use.
* 	Now we only need to pass the DM object as reference to the classes.
* 	Evetually - it would be wise to have classes extend the DM class as oposed to
* 	getting it by reference. This makes for cleaner and more reliable code.
*
* @package core
* @version 0.6.4
*/
class dm{
	/**
	* Toggle for script debugging on error messages (use true only in development)
	*@var boolean
	*/
	var $scriptDebugging = false;
	/**
	* Toggles to kill the script execution when an error occurs.
	*@var boolean
	*/
	var $killScriptOnError = false;

	/**
	* Contrusctor class for DM - Sets the initial values from Config file
	* @return void
	*/
	function dm(){
		// check to see if the CONFIGURATION is loaded
		$this->scriptDebugging = ERROR_DEBUGGING;
		$this->killScriptOnError=STOP_EXECUTION_ON_ERROR;
	}

	/**
	*	Display debug information about the a current object.
	*	@return Stirng
	*	@param Object $thing Any object that requires debugging
	*	@param Boolean $killScriptOnError Toggle if the script should stop executing (defaults for false)
	*/
	function debug($thing,$killScriptOnError=false){

		echo "<table border=0 cellpadding='4' cellspacing='1' bgcolor='#000000' width='100%'>
				<tr>
					<td bgcolor=#FFFFFF>
						<B style='font-size:8pt;font-family:sans-serif; color:#FF5500'>CAUTION: THIS OPTION SHOULD NEVER BE ENABLED IN A LIVE SITE!</B>
					</td>
				</tr>
				<tr><td bgcolor=#FFFFFF style='font-size:8pt;font-family:sans-serif; color:#000000'>Potential security information may be transmitted over debug values.</td></tr>
				<tr><td bgcolor=#EEEEEE style='font-size:10pt;font-family:sans-serif; color:#000000'>DEBUG OUTPUT</td></tr>
				<tr><td bgcolor=#FFFFFF><PRE style='font-size:8pt;font-family:courier,mono; color:#000099'>";

		if (is_string($thing)){
			echo $thing;
		}else{
			//var_dump($thing);
			print_r($thing);
		}
		echo "\n</PRE></td></tr>";
		echo "</table>";
		if ($killScriptOnError || $this->killScriptOnError){
			echo "<span style='font-face:sans-serif;font-size:10px'>Execution HALTED by user demand</span><BR><BR>";
			die();
		}
	}
	function watch($result,$obj="",$operation=""){
		if ($result){
			if ($this->debugging){
				echo "<div style='font-size:10px;font-family:arial,sans;color:#FFFFFF;background-color:#008800'>OK: ";
				echo strtoupper($this->db_type)."($operation) - [No errors computed] ";
				echo "</div>";
			}
			return true;
		}else{
			if ($this->debugging){
				echo "<div style='font-family:arial,sans;color:#FFFFFF;background-color:#ff0000;font-size:10px;'>ERROR: ";
				echo strtoupper($obj)."($operation) - [".$obj->errDesc."] ";
				echo "</div>";
			}
			return false;
		}

	}
	/**
	* Display a formatted error message to the user.
	* @return void
	* @param String $text Message to be displayed
	*/
	function _nice ($text){
		//later here I'll put the external file error handling routines.. for
		// now We presume that all errors are using a the default_message

		echo ERROR_DEFAULT_MESSAGE."<BR>";
		echo "<span style='font-face:sans-serif;font-size:12px'>".$_SERVER['PHP_SELF']."</span><BR>";
		echo "<span style='font-face:sans-serif;font-size:10px'>ERRO: $text</span><BR>";
		echo "<span style='font-face:sans-serif;font-size:10px'>Se o error persistir, envie um e-mail para o <a href='mailto:".WEBMASTER."'>Webmaster</a> e copie este erro na mensagem.</span><BR><BR>";
		if ($this->killScriptOnError){
			echo "<span style='font-face:sans-serif;font-size:10px'>A execucao da pagina foi interrompida.</span><BR><BR>";
			die();
		}

	}

	/**
	* Report an error - Will either display the _nice or _nice and Debug depending on parameters set
	* @return Void
	* @param String $message
	* @param Int $debugValue
	*/
	function errorReport($message,$debugValue=""){

		if (is_string($message)){
			$this->_nice($message);
		}
		if ((ERROR_DEBUGGING) || $debugValue!=""){
			$this->debug($debugValue);
		}

	}

	/**
	* Load a specified class from a specified package in fhte resource/classes directory
	* @return void
	* @param String $packageName Name of the package to load from
	* @param String $className Name of the class to load
	* @param String $constructor If the class has no constructor, this should be the name of a funcion inside. (defaults to the class constructor)
	*/
	function loadClass($packageName,$className,$constructor=""){
		if ($constructor==""){
			$constructor=$className;
		}
		if(!is_callable(array($className,$constructor))){
			if (defined("DIR_CLASS")){
				$pkg = DIR_CLASS.$packageName."/";
				if (is_dir($pkg)){
					// we have a directory with the package name
					$cls = $pkg.$className.".class.php";
					if(is_file($cls)){
						// we have a file
						include_once($cls);
					}else{
						$this->killScriptOnError=true;
						$this->errorReport("Class <b>$className</b> could not be found in package <b>$packageName</b> , please check your instalation");
					}
				}else{
					$this->killScriptOnError=true;
					$this->errorReport("Package <b>$packageName</b> could not be found, please check your instalation");
				}
			}
		}
	}
}
?>